import { filterObj } from '@/utils/util';
import { deleteAction, getAction,downFile,getFileAccessHttpUrl,httpAction } from '@/api/manage'
import Vue from 'vue'
import { ACCESS_TOKEN } from "@/store/mutation-types"
import debounce from 'lodash/debounce';

import {keyTips} from '@/views/menzhen/mock.js'
// 定义键盘码常量以提高可读性和可维护性
const KEY_CODE_DOWN = 40;
const KEY_CODE_UP = 38;
const KEY_CODE_SPACE = 32;
const KEY_CODE_LEFT = 37;
const KEY_CODE_RIGHT = 39;
const KEY_CODE_ENTER = 13;
const modalMinxin = {
  data() {
    return {
      keyTips,
      keyDownObj:{
        autoIndex:0,
        autoHaoCaiIndex:1,
        KEY_CODE:'',
        KEY_CODE_DOWN:KEY_CODE_DOWN,
        KEY_CODE_UP:KEY_CODE_UP,
        KEY_CODE_SPACE:KEY_CODE_SPACE,
        KEY_CODE_LEFT:KEY_CODE_LEFT,
        KEY_CODE_RIGHT:KEY_CODE_RIGHT,
        KEY_CODE_ENTER:KEY_CODE_ENTER,
      },
      tableDom:'#listTable',
    }
  },
 
  mounted() {
    
  },
  watch: {
   
  },
  beforeDestroy(){
   
  },
  distroy(){
      document.removeEventListener('keydown',this.handleKeyPress);
  },
  methods: {
    handleKeydown(event) {
      console.log(event,'event.handleKeydown');
      // 检查按下的是否为箭头方向键
      if (event.keyCode === 37 || event.keyCode === 38 || event.keyCode === 39 || event.keyCode === 40) {

        // 阻止默认行为，即禁用箭头方向键的导航功能
        event.preventDefault();
      }
    },
    handleKeyPress(e){
        var that = this;
        // console.log(e.keyCode ,this.keyDownObj.autoIndex,'handleKeyPress')
        // 回车
        if (e.keyCode === this.keyDownObj.KEY_CODE_ENTER && this.visible) {
          //如果是搜索 是聚焦状态，回车就是 筛选操作
          var inputFocus  = $('#yaofangform input').is(':focus')
          if(inputFocus){
            return
          }
          setTimeout(() => {
            //耗材单独处理
            if (this.seeDetail) {
              const obj = this.dataSource[this.keyDownObj.autoIndex];
              this.handleDetail(obj);
            } else {
              this.handleOk();
            }
          }, 200);
        }
         //向下
        if (e.keyCode == this.keyDownObj.KEY_CODE_DOWN) {
          this.keyDownObj.KEY_CODE = 'KEY_CODE_DOWN'
          $('#yaofangform').mouseleave()
          $('#yaofangform input') && $('#yaofangform input').blur()
          if(this.keyDownObj.autoIndex >= that.dataSource.length - 1){
            if($('.ant-pagination-next').hasClass('ant-pagination-disabled')){
              // this.$message.warning('已是最后一页！')
              return
            }
            
            this.keyChangePage('.ant-pagination-next')
            this.keyDownObj.autoIndex = 0;
          
          }else{
            this.removeActClassFromAllRows();
            if($('.van-toast').is(':visible')){
              return
            }
            if(this.multiple){
              this.selectedRowKeys=[]
              this.selectedTable.dataSource=[]
            }
            this.keyDownObj.autoIndex++
            this.setActiveRowByIndex(this.keyDownObj.autoIndex);
          }
         
        }
        //向上
        if (e.keyCode == this.keyDownObj.KEY_CODE_UP) {
          this.keyDownObj.KEY_CODE = 'KEY_CODE_UP'
          $('#yaofangform input') && $('#yaofangform input').blur()
          if(this.keyDownObj.autoIndex <= 0){
            if(this.ipagination.current == 1){
              // this.$message.warning('已是第一页！')
              return
            }
            
            this.keyChangePage('.ant-pagination-prev')
            this.keyDownObj.autoIndex = that.ipagination.pageSize - 1;
            return
          }else{
            this.removeActClassFromAllRows();
            if($('.van-toast').is(':visible')){
              return
            }
            if(this.multiple){
              this.selectedRowKeys=[]
              this.selectedTable.dataSource=[]
            }
            this.keyDownObj.autoIndex--
            this.setActiveRowByIndex(this.keyDownObj.autoIndex);
          }
         
        }
        //空格
        // if (e.keyCode == this.keyDownObj.KEY_CODE_SPACE) { 
        //   var $tabTrTarget = $('#listTable').find('tbody.ant-table-tbody tr').eq([this.keyDownObj.autoIndex])
        //   var seedetail = $tabTrTarget.find('.see-detail')
        //   // 如果是有查看 的按钮，（耗材部分的处理）
        //   if(seedetail.length){
        //     var obj = that.dataSource[this.keyDownObj.autoIndex]
            
        //     // debugger
        //     setTimeout(() => {
        //       that.handleDetail(obj)
        //     }, 200);
        //   }else{
        //     $tabTrTarget.click()
        //     setTimeout(() => {
        //       $tabTrTarget.addClass('act')
        //     }, 200);
        //   }
          
        // }
        // 左箭头
        if (e.keyCode == this.keyDownObj.KEY_CODE_LEFT) {
       
          that.keyChangePage('.ant-pagination-prev')
        }
        // 右箭头
        if (e.keyCode == this.keyDownObj.KEY_CODE_RIGHT) {
       
          that.keyChangePage('.ant-pagination-next')
         
        }
    },
    // 提取重复代码到单独的方法中
    removeActClassFromAllRows() {
      // 假设有一个Vue引用指向table的tbody，例如通过ref获取
      $(this.tableDom).find('.ant-table-tbody tr').each(function(){
        $(this).removeClass('act');
      })
    },
    setActiveRowByIndex(index) {
      const row =  $(this.tableDom).find('.ant-table-tbody tr').eq(index)
      // console.log(row,'row')
      var $tabTrTarget = $(this.tableDom).find('tbody.ant-table-tbody tr').eq(index)
      if (row) {
        row.addClass('act');
        $tabTrTarget.click()
      }
    },
    keyChangePage(val){
      if($(this.tableDom).find(val).hasClass('ant-pagination-disabled')){
        return
      }
      this.keyDownObj.autoIndex = 0
      $(this.tableDom).find(val).click();
    }
  }
  
 
}

export { modalMinxin }